package com.boss.salary.dao.impl;

import com.boss.common.util.DateUtils;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.IPersonTypeSalaryDao;
import com.boss.salary.entity.PersonTypeSalary;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class PersonTypeSalaryDaoImpl  extends BaseDaoImpl implements IPersonTypeSalaryDao {
    @Override
    public void savePersonTypeSalary(PersonTypeSalary personTypeSalary) {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into ").append(TableName.SAL_PERSON_TYPE_SALARY);
        sql.append("(id,create_time,update_time," )
            .append("person_type_id,salary_id,column_name,")
            .append("is_deleted,mof_div_code,person_type,")
            .append("person_type_code,salary_name) ");
        sql.append("values (?,?,?,?,?,?,?,?,?,?,?)");
        Object[] params = new Object[]{
                personTypeSalary.getId(), personTypeSalary.getCreateTime(), personTypeSalary.getUpdateTime(),
                personTypeSalary.getPersonTypeId(),personTypeSalary.getSalaryId(), personTypeSalary.getColumnName(),
                personTypeSalary.getIsDeleted(),personTypeSalary.getMofDivCode(), personTypeSalary.getPersonType(),
                personTypeSalary.getPersonTypeCode(),personTypeSalary.getSalaryName()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void updatePersonTypeSalary(PersonTypeSalary personTypeSalary){

    }

    @Override
    public void batchSavePersonTypeSalary(List<PersonTypeSalary> personTypes,String mof_div_code) {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into ").append(TableName.SAL_PERSON_TYPE_SALARY);
        sql.append("(id,create_time,update_time," )
                .append("person_type_id,salary_id,column_name,")
                .append("is_deleted,mof_div_code,person_type,")
                .append("person_type_code,salary_name) ");
        sql.append("values (?,?,?,?,?,?,?,?,?,?,?)");
        List<Object[]> saveList = new ArrayList<>(personTypes.size());
        for (PersonTypeSalary personTypeSalary : personTypes) {
            Object[] objects = new Object[]{
                    SysUtils.generateGuid(), String.valueOf(DateUtils.getCurrentTimeStamp()),String.valueOf(DateUtils.getCurrentTimeStamp()),
                    personTypeSalary.getPersonTypeId(),personTypeSalary.getSalaryId(), personTypeSalary.getColumnName(),
                    SystemDictConstant.IS_FALSE,mof_div_code, personTypeSalary.getPersonType(),
                    personTypeSalary.getPersonTypeCode(),personTypeSalary.getSalaryName(),
            };
            saveList.add(objects);
        }
        this.getJdbcTemplate().batchUpdate(sql.toString(),  new BatchPreparedStatementSetter() {
            public int getBatchSize() {
                return saveList.size();
                //这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
            }
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                Object[] sme = saveList.get(i);
                for(int a = 0; a < 11; a++){
                    ps.setObject(a+1,sme[a]);
                }

            }
        });
    }

    @Override
    public List<PersonTypeSalary> getInfoByPersonTypes(List<String> personTypes,String mof_div_code) {
        List<Object> params=new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append("select  ")
                .append(" distinct " )
                .append("spts.salary_id,spts.column_name,sb.is_compute,")
                .append("spts.person_type,")
                .append("spts.person_type_code,spts.salary_name,sb.show_order from ").append(TableName.SAL_PERSON_TYPE_SALARY).append(" spts ")
                .append(" join ").append(TableName.SALARY_BASEDATA).append(" sb ")
                .append(" on spts.salary_id=sb.id and spts.mof_div_code=sb.mof_div_code ")
                .append(" where spts.mof_div_code=? ")
                .append(" and spts.is_deleted=0 and sb.is_deleted=0 and sb.base_state=1 ");
        params.add(mof_div_code);
        if (!CollectionUtils.isEmpty(personTypes)) {
            sql.append(" and ").append(SysUtils.inSql("spts.person_type_code", personTypes));
        }
        sql.append(" order by sb.show_order");

        return this.getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper<>(PersonTypeSalary.class), params.toArray());
    }
    @Override
    public List<PersonTypeSalary> getInfoByPersonTypes4Unit(List<String> personTypes,String mof_div_code) {
        //该查询进行了工资项去重，不会因为人员类型显示多个相同的工资项
        List<Object> params=new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append("select  ")
                .append(" distinct " )
                .append("spts.salary_id,spts.column_name,sb.is_compute,sb.is_standard,")
                .append("sb.salary_name,sb.show_order,sb.is_show,sb.decimal_place from ").append(TableName.SAL_PERSON_TYPE_SALARY).append(" spts ")
                .append(" join ").append(TableName.SALARY_BASEDATA).append(" sb ")
                .append(" on spts.salary_id=sb.id and spts.mof_div_code=sb.mof_div_code ")
                .append(" where spts.mof_div_code=? ")
                .append(" and spts.is_deleted=0 and sb.is_deleted=0 and sb.base_state=1 ");
        params.add(mof_div_code);
        if (!CollectionUtils.isEmpty(personTypes)) {
            sql.append(" and ").append(SysUtils.inSql("spts.person_type_code", personTypes));
        }
        sql.append(" order by sb.show_order");

        return this.getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper<>(PersonTypeSalary.class), params.toArray());
    }
    @Override
    public void physicsDeletePersonTypeSalary(Map<String, Object> queryParam)  throws Exception  {
        StringBuilder sql = new StringBuilder();
        List<Object> params=new ArrayList<>();
        sql.append(" delete from ").append(TableName.SAL_PERSON_TYPE_SALARY);
        sql.append(" where 1 = 1");
        if (!StringUtils.isEmpty(queryParam) && queryParam.size() > 0) {
            // 包含工资信息，根据工资区划删除
            if (queryParam.containsKey("mof_div_code")) {
                String mof_div_code = queryParam.get("mof_div_code").toString();
                sql.append(" and mof_div_code = ? ");
                params.add(mof_div_code);
            }else{
                throw  new Exception("获取人员类型挂接工资项区划信息异常");
            }
            // 根据id删除
            if (queryParam.containsKey("id")) {
                String id = queryParam.get("id").toString();
                sql.append(" and id = ? ");
                params.add(id);
            }
            // 根据人员类型删除
            if (queryParam.containsKey("personTypeCode")) {
                String personTypeCode = queryParam.get("personTypeCode").toString();
                sql.append(" and person_type_code = ? ");
                params.add(personTypeCode);
            }

        }else{
            throw  new Exception("识别人员类型挂接工资项信息错误");
        }
        this.getJdbcTemplate().update(sql.toString(),params.toArray());
    }

    @Override
    public List<Map<String, Object>> getAllPersonTypeByProvince(String mof_div_code) {

        StringBuilder sql = new StringBuilder();
        List<Object> params=new ArrayList<>();
        sql.append(" select id as \"personTypeId\", code as \"personTypeCode\", name \"personTypeName\" from ")
                .append(TableName.BASE_DATA);
        sql.append(" where mof_div_code = ? and type='salary_type' and is_deleted=0 ");
        params.add(mof_div_code);
        return this.getjdbcTemplate().queryForList(sql.toString(),params.toArray());
    }

    @Override
    public List<String> getAllPersonTypeByAgencyId(String agencyid) {
        StringBuilder sql = new StringBuilder();
        List<Object> params=new ArrayList<>();
        sql.append(" select distinct merit_pay_code as personTypeId from ")
                .append(TableName.PERSON_EXT);
        sql.append(" where agency_id = ? and is_deleted=0 ");
        params.add(agencyid);
        return super.getjdbcTemplate().queryForList(sql.toString(), params.toArray(), String.class);
    }

    @Override
    public List<Map<String, Object>> selectSalaryName(String mof_div_code) {
        StringBuilder sb = new StringBuilder();
        sb.append(" select ")
                .append(" sba.salary_name \"key\", sba.salary_code \"value\" , is_compute \"iscompute\"")
                .append(" from ")
               // .append(TableName.SAL_PERSON_TYPE_SALARY).append(" asy ")
                //.append(" left join ")
                .append(TableName.SALARY_BASEDATA).append(" sba ")
                //.append(" on ").append(" asy.salary_id = sba.id ")
                .append(" where ")
                .append(" sba.is_deleted = 0 and sba.base_state = '1' and sba.mof_div_code =  ?  ")
               // .append(" and asy.is_deleted = 0 and asy.mof_div_code =  ? ")
                .append(" order by sba.show_order ");
        return getjdbcTemplate().queryForList(sb.toString(), mof_div_code);
    }
}
